#!/usr/bin/env bash

if [ -z "$STANDALONE" ]; then
	if [ ! -f config.mak ]; then
		echo "run ./configure && make first. See ./configure -h"
		exit 2
	fi
	source config.mak
	source scripts/arch-run.bash
fi

ACCEL=$(get_qemu_accelerator) ||
	exit $?

qemu=$(search_qemu_binary) ||
	exit $?

if ! $qemu -machine '?' 2>&1 | grep 'pseries' > /dev/null; then
	echo "$qemu doesn't support pSeries ('-machine pseries'). Exiting."
	exit 2
fi

M='-machine pseries'
M+=",accel=$ACCEL"
command="$qemu -nodefaults $M -bios $FIRMWARE"
command+=" -display none -serial stdio -kernel"
command="$(migration_cmd) $(timeout_cmd) $command"

# powerpc tests currently exit with rtas-poweroff, which exits with 0.
# run_qemu treats that as a failure exit and returns 1, so we need
# to fixup the fixup below by parsing the true exit code from the output.
# The second fixup is also a FIXME, because once we add chr-testdev
# support for powerpc, we won't need the second fixup.
run_qemu_status $command "$@"
